package com.dongbo.recorder.screenrecord.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupPosition;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.umeng.analytics.MobclickAgent;
import com.dongbo.recorder.screenrecord.R;
import com.dongbo.recorder.screenrecord.adapter.MyPageAdapter;
import com.dongbo.recorder.screenrecord.base.BaseFragment;
import com.dongbo.recorder.screenrecord.contents.Contents;
import com.dongbo.recorder.screenrecord.utils.eventbus.EventMessage;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class FileFragment extends BaseFragment {

    @BindView(R.id.files_menu)
    TextView filesMenu;
    @BindView(R.id.files_tab)
    TabLayout filesTab;
    @BindView(R.id.files_vp)
    ViewPager filesVp;
    @BindView(R.id.files_cannel)
    TextView filesCannel;

    private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
    //    private String[] strings = new String[]{"我的视频", "我的截图"};
    private String[] strings = new String[]{"我的视频"};

    //模式选择 0无 1编辑 2拼接
    private int modelType = 0;
    //视频列表fragment
    private VideoListFragment videoListFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_files;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        MyPageAdapter fragmentAdater = new MyPageAdapter(getChildFragmentManager(), strings, fragmentList);
        videoListFragment = new VideoListFragment();
        fragmentList.add(videoListFragment);
//        fragmentList.add(new PicListFragment());
        filesVp.setAdapter(fragmentAdater);
        filesTab.setupWithViewPager(filesVp);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        filesTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().equals(strings[0])) {
                    setModel(modelType);
                } else {
                    filesMenu.setVisibility(View.GONE);
                    filesCannel.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @OnClick({R.id.files_cannel, R.id.files_menu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.files_cannel:
                setModel(0);
                videoListFragment.changeModel(0);
                break;
            case R.id.files_menu:
                if (modelType == 0) {
                    new XPopup.Builder(getActivity())
                            //.maxWidth(600)
                            .atView(requireActivity().findViewById(R.id.files_menu))
                            .popupPosition(PopupPosition.Right)
                            .asAttachList(new String[]{getResources().getString(R.string.text_video_edit),
                                            getResources().getString(R.string.text_video_splicing)}, new int[]{R.mipmap.ic_jianji, R.mipmap.ic_pinjie},
                                    new OnSelectListener() {
                                        @Override
                                        public void onSelect(int position, String text) {
                                            if (position == 0) {
                                                setModel(1);
                                                videoListFragment.changeModel(1);
                                            } else {
                                                setModel(2);
                                                videoListFragment.changeModel(2);
                                            }
                                        }
                                    })
                            .show();
                } else if (modelType == 2) {
                    videoListFragment.changeModel(3);
                }
                break;
        }
    }

    //模式设置 0无 1编辑 2拼接
    public void setModel(int model) {
        this.modelType = model;
        switch (modelType) {
            case 0:
                filesMenu.setText(R.string.text_choose);
                filesMenu.setVisibility(View.VISIBLE);
                filesCannel.setVisibility(View.GONE);
                break;
            case 1:
                filesMenu.setVisibility(View.GONE);
                filesCannel.setVisibility(View.VISIBLE);
                break;
            case 2:
                filesCannel.setVisibility(View.VISIBLE);
                filesMenu.setVisibility(View.VISIBLE);
                filesMenu.setText(R.string.text_splicing);
                break;
        }
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    /**
     * 接收到推送的事件
     *
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPushReceiveEvent(final EventMessage event) {
        //截屏成功
        if (event.getCode() == Contents.EventType.TAG_SHOT_SUCCESS) {
            boolean isRecordSuecss = (boolean) event.getData();
            if (isRecordSuecss) {
                filesVp.setCurrentItem(1);
            }
            //录屏成功
        } else if (event.getCode() == Contents.EventType.TAG_RECORD_SUCCESS) {
            boolean isRecordSuecss = (boolean) event.getData();
            if (isRecordSuecss) {
                filesVp.setCurrentItem(0);
            }
        }
    }

    // Fragment页面onResume函数重载
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("FileFragment"); //统计页面("MainScreen"为页面名称，可自定义)
    }

    // Fragment页面onResume函数重载
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("FileFragment");
    }

}
